//
// Created on 2024/11/27.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef SLOTDRAWING_ANIMATE_H
#define SLOTDRAWING_ANIMATE_H

#include "ImageNode.h"
#include <js_native_api.h>
#include <cstdint>
#include "ArkUICustomNode.h"
#include <arkui/native_animate.h>
#include "ArkUICustomContainerNode.h"

class Animate {
public:
    int32_t eCode = 0;
    int32_t duration = 1000;
    ArkUI_AnimationCurve easing = ARKUI_CURVE_LINEAR;
    int32_t delay = 0;
    ArkUI_AnimationDirection direction = ARKUI_ANIMATION_DIRECTION_NORMAL;
    int32_t iterations = 1;
    float begin = 0;
    float end = 1;
    ArkUI_ExpectedFrameRateRange frameRate = {.min = 20, .max = 120, .expected = 60};
    ArkUI_AnimationFillMode fill = ARKUI_ANIMATION_FILL_MODE_FORWARDS;
    ArkUI_AnimatorOption *AnimatorOption = nullptr;
    ArkUI_AnimatorHandle AnimatorHandle;
    ArkUI_NativeAnimateAPI_1 *AnimateAPi_1 = nullptr;

    std::shared_ptr<NativeModule::ArkUICustomNode> GetNode;
    std::shared_ptr<NativeModule::ImageNode> GetImageNode;
//    void test()
//    {
//        GetImageNode->PlayFrame();
//    }

    void test()
    {
        GetNode->setFrameNum();
    }

    void SetNode(std::shared_ptr<NativeModule::ImageNode> Node)
    {
        GetImageNode = Node;
    }

    void SetNode(std::shared_ptr<NativeModule::ArkUICustomNode> Node)
    {
        GetNode = Node;
    }

    Animate(ArkUI_ContextHandle context)
    {

        // 创建ArkUI_AnimatorHandle
        AnimateAPi_1 = reinterpret_cast<ArkUI_NativeAnimateAPI_1 *>(
            OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_ANIMATE, "ArkUI_NativeAnimateAPI_1"));
        if (AnimateAPi_1 != nullptr) {
            DRAWING_LOGE("\n\n\nAnimateAPi_1 地址%{public}p", AnimateAPi_1);
        }

        // 创建动画控制选项
        AnimatorOption = OH_ArkUI_AnimatorOption_Create(0);

        // 设置animator动画持续时间
        eCode = OH_ArkUI_AnimatorOption_SetDuration(AnimatorOption, duration);
        DRAWING_LOGE("\n\n\n设置animator动画持续时间 错误代码%{public}d", eCode);

        // 设置animator动画延迟时间
        eCode = OH_ArkUI_AnimatorOption_SetDelay(AnimatorOption, delay);
        DRAWING_LOGE("\n\n\n设置animator动画期望延迟时间 错误代码%{public}d", eCode);

        // 设置animator动画播放次数
        eCode = OH_ArkUI_AnimatorOption_SetIterations(AnimatorOption, iterations);
        DRAWING_LOGE("\n\n\n设置animator动画播放次数 错误代码%{public}d", eCode);

        // 设置animator动画遮罩
        eCode = OH_ArkUI_AnimatorOption_SetFill(AnimatorOption, fill);
        DRAWING_LOGE("\n\n\n设置animator动画动画遮罩 错误代码%{public}d", eCode);

        // 设置animator帧率范围
        eCode = OH_ArkUI_AnimatorOption_SetExpectedFrameRateRange(AnimatorOption, &frameRate);
        DRAWING_LOGE("\n\n\n设置animator动画帧率范围 错误代码%{public}d", eCode);

        // 设置animator 帧回调
        eCode = OH_ArkUI_AnimatorOption_RegisterOnFrameCallback(AnimatorOption, this, onCallback);
        DRAWING_LOGE("\n\n\n设置animator动画帧回调 错误代码%{public}d", eCode);

        // 设置animator播放方向
        eCode = OH_ArkUI_AnimatorOption_SetDirection(AnimatorOption, direction);
        DRAWING_LOGE("\n\n\n设置animator动画播放方向 错误代码%{public}d", eCode);

        // 设置animator ArkUI_ContextHandle
        if (AnimateAPi_1 != nullptr && AnimateAPi_1->createAnimator != nullptr) {
            AnimatorHandle = AnimateAPi_1->createAnimator(context, AnimatorOption);
        }
    }

    static void onCallback(ArkUI_AnimatorOnFrameEvent *event)
    {
        Animate *data = reinterpret_cast<Animate *>(OH_ArkUI_AnimatorOnFrameEvent_GetUserData(event));
        data->test();
//         DRAWING_LOGE("\n\n\n运行次数%{public}d",RunNum);
    }

    void Play()
    {
        // 启动animator动画
        eCode = OH_ArkUI_Animator_Play(AnimatorHandle);
        DRAWING_LOGE("\n\n\n启动animator动画%{public}d", eCode);
    }

    void Stop()
    {
        eCode = OH_ArkUI_Animator_Finish(AnimatorHandle);
        DRAWING_LOGE("\n\n\n启动animator动画%{public}d", eCode);
    }
};

#endif // SLOTDRAWING_ANIMATE_H
